#include <algorithm/analytical_model/io/json/converter/shared/data/topology/geometry/Geometry.h>
#include <algorithm/analytical_model/io/json/converter/shared/data/topology/geometry/Rectangle.h>
#include <algorithm/analytical_model/io/json/converter/shared/utils/Vec2.h>

using namespace solar;

void solar::tag_invoke(const boost::json::value_from_tag& /*unused*/,
                       boost::json::value& json_value, const Rectangle& rectangle)
{
    auto size = SOLAR_GET_VALUE(Rectangle, size_, rectangle);
    constexpr auto type = io::json::internal::convertGeometryType(GeometryType::kGeometryRectangle);
    json_value = {
        {"type",                          type},
        {"size", boost::json::value_from(size)},
    };
}

auto solar::tag_invoke(const boost::json::value_to_tag<Rectangle>& /*unused*/,
                       const boost::json::value& json_value) -> Rectangle
{
    const auto* value_ptr = json_value.if_object();
    if (value_ptr == nullptr)
    {
        return {};
    }
    Rectangle result;
    if (const auto* size_value = value_ptr->if_contains("size"); size_value != nullptr)
    {
        SOLAR_SET_VALUE(Rectangle, size_, boost::json::value_to<Vec2>(*size_value), result)
    }
    return result;
}
